package com.cxj.cxj_test_cloud.common.lifecycles;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cxj.cxj_test_cloud.utils.Logger;

import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

public class ActivityLifeCycles implements Application.ActivityLifecycleCallbacks{

    WeakHashMap<Activity,Activity> activityWeakHashMap=new WeakHashMap<>();
    private WeakReference<Activity> createActivityWeakRef;
    private WeakReference<Activity> curActivityWeakRef;
    private static Handler mainHandler=new Handler(Looper.getMainLooper());

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
        activityWeakHashMap.put(activity,activity);
        createActivityWeakRef = new WeakReference<>(activity);

        if (BuildConfig.DEBUG){
            //调试时打印栈信息
            printActivityStack();
        }
    }




    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        Logger.i("ActivityLifeCycles", activity.getClass().getSimpleName() + " onStarted");
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        Logger.i("ActivityLifeCycles", activity.getClass().getSimpleName() + " onResumed");
        curActivityWeakRef = new WeakReference<>(activity);

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
        Logger.i("ActivityLifeCycles", activity.getClass().getSimpleName() + " onPaused");
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        Logger.i("ActivityLifeCycles", activity.getClass().getSimpleName() + " onStopped");
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
        Logger.i("ActivityLifeCycles", activity.getClass().getSimpleName() + " onSaveInstanceState");
    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        activityWeakHashMap.remove(activity);
    }

    public static Handler getMainHandler(){
        return mainHandler;
    }

    public static Activity getActivity(){
        Activity curAct;
        if (Precondition.checkActivityReference(curActivityWeakRef)) {
            //当前的不为空
            curAct = curActivityWeakRef.get();
            if (Precondition.checkActivity(curAct)) {
                return curAct;
            } else {
                curAct = createActivityWeakRef.get();
                if (Precondition.checkActivity(curAct)) {
                    return curAct;
                }
            }
        } else {
            return createActivityWeakRef.get();
        }
        return curAct;
    }

    private void printActivityStack() {
        Logger.d("ActivityStack","当前Activity栈大小："+activityWeakHashMap.size());
        for (Activity activity : activityWeakHashMap.keySet()) {
            Logger.d("ActivityStack","当前Activity栈大小："+activity.getClass().getSimpleName());
        }

    }
}
